<template>
  <basic-container>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-row>
        <el-form-item label="项目编号:">
          <el-input v-model="query.projectCode" clearable placeholder="项目编号" size="small"></el-input>
        </el-form-item>
        <el-form-item label="项目名称:">
          <el-input v-model="query.projectName" clearable placeholder="项目名称"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-row>
    </el-form>
<!--    <div class="btn-operate">-->
<!--      <el-button @click="addReportForm" type="primary" size="small" icon="el-icon-plus">-->
<!--        整改报告-->
<!--      </el-button>-->
<!--    </div>-->
    <el-table v-loading="loading" :data="data" border
              highlight-current-row @cell-click="handleCellClick" style="width: 100%">
      <el-table-column prop="projectCode" label="项目编号"></el-table-column>
      <el-table-column prop="projectName" label="项目名称"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0 || scope.row.status === null">新建</span>
          <span v-else-if="scope.row.status === 1">已提交</span>
          <span v-else-if="scope.row.status === 2">驳回</span>
          <span v-else-if="scope.row.status === 3">已确认</span>
          <span v-else>未知状态</span>
        </template>
      </el-table-column>
      <el-table-column prop="pendingCount" label="已整改">
      </el-table-column>
      <el-table-column prop="solvedCount" label="未整改">
      </el-table-column>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-view" @click="showViewBox(scope.row)">查看</el-button>
          <el-button v-if="permissions.rectifyReport_add && (scope.row.status === 0 || scope.row.status === 2)" type="text" size="small" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑</el-button>
          <el-button v-if="permissions.rectifyReport_audit && scope.row.status === 1" type="text" size="small" icon="el-icon-edit" @click="showAuditBox(scope.row)">审核</el-button>
          <!--          <el-button v-if="scope.row.taskId" type="text" size="small" icon="el-icon-edit" @click="showTaskAuditTaskDialog(scope.row)">审批</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40 ,50 ,100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog title="整改报告" fullscreen custom-class="ledger" :visible.sync="auditBox" :close-on-click-modal="!auditBox" :append-to-body="true" :before-close="cancelDialog" width="1200px" top="5vh">
      <div class="colums">
        <div class="colums_left">
          <div class="colums_left_middle">
            <div class="middleForm ledger">
              <el-form ref="dataForm" :model="form" class="el-dialog__body" label-width="100px">
                <div class="title">整改报告</div>
                <el-row>
                  <el-col>
                    <el-form-item label-width="140px" label="整改报告:">
                      <web-document :form-data="form" :readWord="'view'" :pageType="'rectifyReport'" @createWordFile="wordFileHandle" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <div class="title">整改情况</div>
                <el-row>
                  <el-col>
                    <el-table
                      ref="table"
                      :data="tableData"
                      :model="tableData"
                      style="width: 100%"
                      max-height="450"
                      highlight-current-row
                      align="center"
                    >
                      <el-table-column type="selection" width="30"></el-table-column>
                      <el-table-column label="序号" width="50">
                        <template #default="{ $index }">
                          <span>{{ $index + 1 }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="问题标题" width="300">
                        <template #default="{ row }">
                          <pre class="pre-style">{{ row.problemTitle }}</pre>
                        </template>
                      </el-table-column>
                      <el-table-column label="整改措施">
                        <template #default="{ row }">
                          <pre class="pre-style">{{ row.rectificationMeasures }}</pre>
                        </template>
                      </el-table-column>
                      <el-table-column label="完成情况" width="120">
                        <template #default="{ row }">
                          <el-select v-model="row.rectifyStatus" placeholder="请选择">
                            <el-option v-for="item in dict.type.ectification_situation" :key="item.value" :label="item.label" :value="parseInt(item.value)"></el-option>
                          </el-select>
                        </template>
                      </el-table-column>
                      <el-table-column label="佐证资料" width="280">
                        <template #default="{ row, $index }">
                          <el-upload
                            class="upload-demo"
                            ref="upload"
                            action="/admin/sys-file/upload"
                            :headers="{Authorization}"
                            :on-preview="handlePreview"
                            :show-file-list="true"
                            :file-list="row.fileList">
                            <i class="el-icon-paperclip" style="color: #409EFF;"></i>
                          </el-upload>
                        </template>
                      </el-table-column>
                    </el-table>
                  </el-col>
                </el-row>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button icon="el-icon-close" size="small" @click="cancelDialog">关闭</el-button>
<!--                <el-button v-if="!isView && form.status === 1" icon="el-icon-check" size="small" type="danger" @click="rejectSubmit" :loading="loading">驳回</el-button>-->
                <el-button v-if="!isView && form.status === 1" icon="el-icon-check" size="small" type="primary" @click="approveSubmit" :loading="loading">确认</el-button>
              </span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog top="5vh" :title="formTitle" :visible.sync="viewBox"
               :append-to-body="true" :before-close="closeViewBox" width="1200px">
      <RectifyReportView :params="form"></RectifyReportView>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeViewBox" size="small" icon="el-icon-close">关闭</el-button>
      </span>
    </el-dialog>
    <table-form ref="reportForm" @cancelDialog="closeReportFormDialog"></table-form>
  </basic-container>
</template>
<script>
import TableForm from "@/components/form/rectifyReprotForm.vue";
import TaskAuditDialog from "@/components/act/TaskAuditDialog.vue";
import {fetchList, getRectifyReport, rejectSubmit, approveSubmit} from "@/api/adis/track/rectifyReprot";
import {mapGetters} from "vuex";
import RectifyReportView from "@/components/actForm/RectifyReportView.vue";
import WebDocument from "@/components/form/webDocument.vue";

export default {
  components: {WebDocument, RectifyReportView, TaskAuditDialog, TableForm},
  computed:{
    ...mapGetters(["permissions"])
  },
  dictTypes: ['audit_type_1_sub','ectification_situation'],
  data(){
    return{
      query:{},
      form:{},
      data:[],
      tableData:[],
      loading:false,
      isView:false,
      viewBox:false,
      auditBox:false,
      page:[
        {
          total:1,
          currentPage:10
        }
      ],
      formTitle: '',
      Authorization: '',
    }
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods:{
    handleCellClick(row, column) {
      if (row && column.label !== "操作"){
        this.showViewBox(row)
      }
    },

    addReportForm(row){
      this.$nextTick(() => {
        this.$refs.reportForm.init(row,true);
      })
    },
    closeReportFormDialog(){
      this.onLoad(this.page);
    },
    onSearch(){
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page)
    },
    showViewBox(row){
      getRectifyReport(row.rectifyId).then((res)=>{
        this.form = res.data.data;
        this.formTitle = row.projectName
        this.viewBox = true;
      })
    },
    closeViewBox() {
      this.viewBox = false;
    },
    showEditBox(row) {
      this.addReportForm(row);
    },
    showAuditBox(row) {
      getRectifyReport(row.rectifyId).then((res)=>{
        this.$set(this, "form", res.data.data);
        console.log(this.form)
        this.form.number = "〔"+this.form.reportYear+"〕"+this.form.reportCode;
        this.tableData = this.form.rectifyReportProblems || []
        this.tableData.forEach(item => {
          item.fileList = []
          item.attachments.forEach(col => {
            item.fileList.push({name: col.fileName, url: col.fileUrl})
          })
        })
        this.auditBox = true;
      });
    },
    onLoad(page, params = {}) {
      this.loading = true;
      // this.query.reportStatus = '初稿';
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    cancelDialog(){
      this.auditBox = false;
      this.onLoad(this.page);
    },
    handlePreview (file) {
      // console.log(file.url);
      const a = document.createElement('a');
      let url = file.url ? file.url : file.response.data.url;
      console.log(url);      // let fileName = '你想要的文件名.docx'; // 这里设置你想要的文件名，包括扩展名
      a.setAttribute('download', file.name);
      a.setAttribute('href', url);
      a.click();
      // window.open(file.url ? file.url : file.response.data.url)
    },
    rejectSubmit(){
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.form.rectifyReportProblems = []
          this.tableData.forEach((item, i) => {
            this.form.rectifyReportProblems.push(item);
          })
          this.loading = true;
          rejectSubmit(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.cancelDialog();
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    approveSubmit(){
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.form.rectifyReportProblems = []
          this.tableData.forEach((item, i) => {
            this.form.rectifyReportProblems.push(item);
          })
          console.log(this.form.rectifyReportProblems,321654)
          this.loading = true;
          approveSubmit(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.cancelDialog();
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    wordFileHandle(e) {
      this.$set(this.form, "wordFile", e.wordFile);
      this.$set(this.form, "reportId", e.id);
    },
  }
}
</script>

